﻿Namespace Dimensions
    Public Class MetisDimensionList
        Inherits MetisSortedEntityList(Of MetisDimension)

        Private _MetisCore As MetisCore

        Sub New(ByVal MetisCore As MetisCore)
            _MetisCore = MetisCore
            Dim OraReader As OracleDataReader
            Dim OraCmd As OracleCommand
            OraCmd = New OracleCommand("SELECT dimension_id, name, description, table_name, modified_on, created_on FROM dam_dimensions ORDER BY name DESC", _MetisCore.Connection)

            OraReader = OraCmd.ExecuteReader
            Do While OraReader.Read
                Dim tmpAnnotation As String = If(Not OraReader.IsDBNull(2), OraReader.GetString(2), "")

                Me.Add(New MetisDimension(_MetisCore, OraReader.GetInt32(0), OraReader.GetString(1), tmpAnnotation, _
                                          _MetisCore.DBTables.FindTable(OraReader.GetString(3)), OraReader.GetDateTime(4), OraReader.GetDateTime(5)))
            Loop
            OraReader.Close()
        End Sub

        Public Overrides Sub Reload()
            Dim tmpDimList As New MetisDimensionList(_MetisCore)
            'Remove deleted Dimensions
            For i As Integer = Me.Count - 1 To 0 Step -1
                If Me(i).ID >= 0 AndAlso tmpDimList.Find(Me(i).ID) Is Nothing Then
                    Me.RemoveAt(i)
                End If
            Next
            'Add new dimensions or update if allready existing (and loaded)
            For Each Item As MetisDimension In tmpDimList
                Dim tmpDim As MetisDimension = Me.Find(Item.ID)
                If tmpDim Is Nothing Then
                    Me.Add(Item)
                ElseIf tmpDim.LoadState = MetisLoadState.Loaded Then
                    tmpDim.Reload()
                End If
            Next
        End Sub


        'Private Sub QuickSort(ByVal Left As Integer, ByVal Right As Integer)
        '    If Left < Right Then
        '        Dim SplitAt As Integer = QSsplit(Left, Right)
        '        QuickSort(Left, SplitAt - 1)
        '        QuickSort(SplitAt + 1, Right)
        '    End If

        'End Sub

        'Private Function QSsplit(ByVal Left As Integer, ByVal Right As Integer) As Integer
        '    Dim i As Integer = Left
        '    Dim j As Integer = Right - 1
        '    Do While i < j
        '        Do While i < Right AndAlso Me(i).Name <= Me(Right).Name
        '            i += 1
        '        Loop

        '        Do While j > Left AndAlso Me(j).Name >= Me(Right).Name
        '            j -= 1
        '        Loop

        '        If i < j Then
        '            Dim tmpDim As MetisDimension = Me(i)
        '            Me(i) = Me(j)
        '            Me(j) = tmpDim
        '        End If
        '    Loop

        '    If Me(i).Name > Me(Right).Name Then
        '        Dim tmpDim As MetisDimension = Me(i)
        '        Me(i) = Me(Right)
        '        Me(Right) = tmpDim
        '    End If

        '    Return i
        'End Function


    End Class
End Namespace

